/**
 * 对账确认单维护
 * @author shixc
 * @date 2019/03/14
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_gwda = {},
        cols = [[
            {field: 'ID', type: 'checkbox', title: '选择', fixed: 'left'}
            , {
                field: 'XX001', width: 120, title: '客户编号', fixed: 'left', sort: true,
                search: 'default', searchOpts: {icon: true,readonly:true}
            }
            , {field: 'COMPANY', width: 80, title: '法人组织', search: 'default', searchOpts: {icon: true}}
            , {
                field: 'XX003_Y', width: 80, title: '对账年度', search: 'default',
                searchOpts: {type: 'year'}, sort: true
            }
            , {
                field: 'XX003_M', width: 80, title: '对账月份', search: 'default',
                searchOpts: {type: 'MM'}, sort: true
            }
            , {field: 'MA083', width: 120, title: '付款条件', search: 'default'}
            , {field: 'AMOUNT', width: 80, title: '上月账期金额'}
            , {field: 'DELIVERY', width: 80, title: '本月账期发货金额'}
            , {field: 'ACCOUNT', width: 80, title: '本月账期应付款额'}
            // , {field: 'CREDIT', width: 80, title: '信用额度'}
            // , {field: 'QC021', width: 80, title: '期初应收金额'}
            // , {field: 'MONEY', width: 80, title: '本期发货金额'}
            // , {field: 'XX016', width: 80, title: '退货金额'}
            // , {field: 'XX010', width: 80, title: '扣账金额'}
            // , {field: 'XX014', width: 80, title: '返利金额'}
            // , {field: 'XX018', width: 80, title: '汇款金额'}
            // , {field: 'AR', width: 80, title: '期末应收金额'}
            , {
                field: 'UDF01', width: 80, title: '确认状态', hide: true, search: 'default',
                searchOpts: {type: 'select', default: ''},
                filterDatas: app.datas_('crm_gwai780_udf01')
            }
            // , {
            //     width: 60,
            //     title: '',
            //     toolbar: '#script-gwai780setting',
            //     fixed: 'right',
            //     align: 'center',
            //     unresize: true
            // }
        ]],
        gwai780SettingAction, gwai780table, company
    ;

    var Gwai780 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai780',
                _searchs: [{
                    id: 'gwai780', tableId: 'gwai780_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'gwai780_edit', tpl: 'gwai780_edit'
                }, {
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }, {
                    dom: 'common_open_gwde', tpl: 'common/_open_gwde'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwai780table = table.render({
                    elem: app.getByid(doc, 'gwai780_table'),
                    url: g_ctxPath + 'crm/gwai780/getList',
                    title: '对账确认单信息表',
                    method: 'post',
                    id: 'gwai780_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'], //, 'exports_import'
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_gwda,
                    done: function (res, curr, count) {

                    }
                });

                var tipsIndex = null;

                //监听行双击事件
                table.on('rowDouble(gwai780_table)', function (obj) {
                    obj.data.COMPANY = company;
                    self._openTplFull({
                        tag: 'gwai780_edit',
                        tagData: obj,
                        index: 0,
                        openOpts: {
                            title: '对账确认单详情',
                            maxmin: true
                        }
                    });
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai780_table', where: table_gwda});
        },

        returns: function () {
            var checkStatus = table.checkStatus('gwai780_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('gwai780');
                    if (v.UDF10 != 'Y') {
                        _temp.TI002 = v.TI002;
                        _temp.COMPANY = company;
                        _xdata.push(_temp);
                    }
                });
                app.ajax({
                    xurl: 'crm/gwai780/refund',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '确认退货成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai780',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'XX001') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.XX001 = data.GWAK001;
                                            vue.isearch.fields.COMPANY = data.GWAKPLANT;
                                            company = data.GWAKPLANT;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'COMPANY') {//法人开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwde003',
                                            searchParam:{
                                                GWDE002 : "ERP账套"
                                            },
                                            columnList: [
                                                {label: '选项值', prop: 'GWDE003', width: '150'},
                                                {label: '选项名称', prop: 'GWDE004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.COMPANY = data.GWDE003;
                                            company = data.GWDE003;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择ERP营运中心',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    },
                    watch: {
                        'isearch.fields.TI003': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                this.isearch.fields.TI003_START = newValue[0].replace(/-/g, '');
                                this.isearch.fields.TI003_END = newValue[1].replace(/-/g, '');
                            } else {
                                this.isearch.fields.TI003_START = '';
                                this.isearch.fields.TI003_END = '';
                            }
                        },
                        'isearch.fields.COMPANY': function (val) {
                            if(val == ''){
                                company = '';
                            }
                        }
                    }
                }]
            });
            self.initTable();
            $('#itoSearch').click(function () {
                if (company == null || company == '') {
                    app.showWarning("法人必选！");
                    return;
                } else if (table_gwda.XX003_Y == undefined) {
                    app.showWarning("年度必选！");
                    return;
                } else if (table_gwda.XX003_M == undefined) {
                    app.showWarning("月份必选！");
                    return;
                } else if(table_gwda.XX001 == undefined) {
                    app.showWarning("客户必选！");
                    return;
                }
            })
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwai780();

});
